import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';


// 在报表页面中使用
void _showYearQuarterPicker(BuildContext context) {
  showModalBottomSheet(
    context: context,
    builder: (context) {
      return YearQuarterPicker(
        initialYear: 2025,
        initialQuarter: 1,
        onConfirm: (year, quarter) {
          // 处理确认选择
          print('选择了 $year 年第 $quarter 季度');
          Navigator.pop(context);
        },
        onCancel: () {
          // 处理取消
          Navigator.pop(context);
        },
      );
    },
  );
}



class YearQuarterPicker extends StatefulWidget {
  final int initialYear;
  final int initialQuarter;
  final Function(int, int) onConfirm;
  final Function() onCancel;

  const YearQuarterPicker({
    Key? key,
    required this.initialYear,
    required this.initialQuarter,
    required this.onConfirm,
    required this.onCancel,
  }) : super(key: key);

  @override
  State<YearQuarterPicker> createState() => _YearQuarterPickerState();
}

class _YearQuarterPickerState extends State<YearQuarterPicker> {
  late int _selectedYear;
  late int _selectedQuarter;

  @override
  void initState() {
    super.initState();
    _selectedYear = widget.initialYear;
    _selectedQuarter = widget.initialQuarter;
  }

  @override
  Widget build(BuildContext context) {
    // 年份数据 (2021-2025)
    final List<int> years = List.generate(5, (index) => 2021 + index);
    // 季度数据 (1-4)
    final List<int> quarters = List.generate(4, (index) => index + 1);

    return Container(
      height: 300,
      color: Colors.white,
      child: Column(
        children:
        [
          // 标题栏
          Container(
            height: 48,
            decoration: const BoxDecoration(
              border: Border(
                bottom: BorderSide(color: Color(0xFFE0E0E0)),
              ),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children:
              [
                TextButton(
                  onPressed: widget.onCancel,
                  child: const Text(
                    '取消',
                    style: TextStyle(
                      color: Color(0xFF41C7DD),
                      fontSize: 16,
                    ),
                  ),
                ),
                const Text(
                  '利润表',
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                TextButton(
                  onPressed: () => widget.onConfirm(_selectedYear, _selectedQuarter),
                  child: const Text(
                    '确认',
                    style: TextStyle(
                      color: Color(0xFF41C7DD),
                      fontSize: 16,
                    ),
                  ),
                ),
              ],
            ),
          ),

          // 选择器标题
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children:
              [
                Text(
                  '$_selectedYear 第$_selectedQuarter季度',
                  style: const TextStyle(
                    color: Colors.black,
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(width: 8),
                const Icon(
                  Icons.arrow_drop_down,
                  color: Colors.grey,
                ),
              ],
            ),
          ),

          // 滚轮选择器
          Expanded(
            child: Row(
              children:
              [
                // 年份选择器
                Expanded(
                  child: CupertinoPicker(
                    scrollController: FixedExtentScrollController(
                      initialItem: years.indexOf(_selectedYear),
                    ),
                    itemExtent: 40,
                    onSelectedItemChanged: (index) {
                      setState(() {
                        _selectedYear = years[index];
                      });
                    },
                    children: years.map((year) {
                      return Center(
                        child: Text(
                          '$year',
                          style: const TextStyle(
                            fontSize: 18,
                            color: Colors.black,
                          ),
                        ),
                      );
                    }).toList(),
                  ),
                ),

                // 分割线
                Container(
                  width: 1,
                  color: const Color(0xFFE0E0E0),
                ),

                // 季度选择器
                Expanded(
                  child: CupertinoPicker(
                    scrollController: FixedExtentScrollController(
                      initialItem: quarters.indexOf(_selectedQuarter),
                    ),
                    itemExtent: 40,
                    onSelectedItemChanged: (index) {
                      setState(() {
                        _selectedQuarter = quarters[index];
                      });
                    },
                    children: quarters.map((quarter) {
                      return Center(
                        child: Text(
                          '第$quarter季度',
                          style: const TextStyle(
                            fontSize: 18,
                            color: Colors.black,
                          ),
                        ),
                      );
                    }).toList(),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}